package com.qwc.blog.web.admin;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qwc.blog.common.annotation.AdminController;
import com.qwc.blog.entity.SysPicture;
import com.qwc.blog.entity.dto.SysPictureDto;
import com.qwc.blog.entity.dto.SysPictureRequestDto;
import com.qwc.blog.service.ISysPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qwc
 * @since 2022-03-04
 */
@AdminController("/sysPicture")
public class SysPictureController {

    @Autowired
    private ISysPictureService sysPictureService;

    @PostMapping
    public ResponseEntity<Object> add(@RequestBody @Validated SysPictureDto dto){
        SysPicture sysPicture = new SysPicture();
        BeanUtil.copyProperties(dto,sysPicture);
        return ResponseEntity.ok(sysPictureService.save(sysPicture));
    }

    @GetMapping("page")
    public ResponseEntity<Object> page(SysPictureRequestDto<SysPicture> requestDto){
        return ResponseEntity.ok(sysPictureService.page(requestDto, Wrappers.<SysPicture>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(requestDto.getImageName()), SysPicture::getImageName,requestDto.getImageName())));
    }

    @PutMapping("/{pictureId}/{imageName}")
    public ResponseEntity<Object> update(@PathVariable Integer pictureId,@PathVariable String imageName){
        return ResponseEntity.ok(sysPictureService.update(Wrappers.<SysPicture>lambdaUpdate()
                .set(SysPicture::getImageName,imageName)
                .eq(SysPicture::getPictureId,pictureId)));
    }

    @DeleteMapping("/{ids}")
    public ResponseEntity<Object> delete(@PathVariable  @NotNull(message = "请选择删除数据") Integer[] ids){
        return ResponseEntity.ok(sysPictureService.removeByIds(Arrays.asList(ids)));
    }

}
